﻿package 
{
	import gameStage;
	
	public class Hand
	{
		private var mRank:int = 0;
		private var temp:int = 0;
		private var hand:Array;
		
		// Params: Card String
		// Return: n/a
		// DESC: Constuctor for the Hand object.
		public function Hand(cards:String)
		{
			this.hand = new Array();
			//splits the card string and places it in an array
			for(var i:int = 0; i<cards.length; i++)
			{
				this.hand[i] = cards.charAt(i);
			}
			//determines the rank of each card in hand
			var rhand = new Array(
			int(gameStage.rankArr.indexOf(this.hand[0]+this.hand[1], 0)),
			int(gameStage.rankArr.indexOf(this.hand[2]+this.hand[3], 0)),
			int(gameStage.rankArr.indexOf(this.hand[4]+this.hand[5], 0)),
			int(gameStage.rankArr.indexOf(this.hand[6]+this.hand[7], 0)),
			int(gameStage.rankArr.indexOf(this.hand[8]+this.hand[9], 0)));
			//determines the highest and second highest rank cards
			for (var j:int = 0; j<rhand.length; j++)
			{
				if (rhand[j] > this.temp)
				{
					this.temp = rhand[j];
					if (rhand[j] > mRank)
					{
						this.temp = mRank;
						mRank = rhand[j];
					}
				}
			}
			//sort hand array for comparison
			this.hand.sort();
			//determines a rank value for the hand
			setRank();
		}
		
		// Params: n/a
		// Return: void
		// DESC: Checks the type of hand: Straight, Flush, etc. Assigns a rank value. 
		//       (extended part of the constructor)
		private function setRank():void
		{
			if(isFlush() != 0)
			{
				mRank = mRank+60;
				//check for Straight-Flush
				if(isStraight() != 0)
				{
					mRank = mRank+150;
				}
			}
			else if(isStraight() != 0)
			{
				mRank = mRank+0;
			}
			else if(isFullHouse() != 0)
			{
				mRank = isFullHouse()+120;
			}
			else if(isFourOfAKind() != 0)
			{
				mRank = isFourOfAKind()+140;
			}
			else
			{
				mRank = 0;
			}
		}

		// Params: n/a
		// Return: rank value of hand, else '0'
		// DESC: Validates the hand as a Flush and returns a rank value. 
		private function isFlush():int
		{
			//creates flush string
			var fhand:String = this.hand[5]+this.hand[6]+this.hand[7]+this.hand[8]+this.hand[9];
			//compares flush string, return result
			if(fhand == "ddddd" || fhand == "ccccc" || fhand == "hhhhh" || fhand == "sssss")
			{
				return mRank;
			}
			return 0;
		}

		// Params: n/a
		// Return: rank value of hand, else '0'
		// DESC: Validates the hand as a Straight and returns a rank value. 
		private function isStraight():int
		{
			//creates straight string
			var shand:String = this.hand[0]+this.hand[1]+this.hand[2]+this.hand[3]+this.hand[4];
			//compares straight string, return result
			if(shand == "34567" || shand == "45678" || shand == "56789" || shand == "0789J" || shand == "089JQ" || shand == "09JKQ" || shand == "01JKQ")
			{
				return mRank;
			}
			else if(shand == "23456")
			{
				mRank = mRank - 36;
				return mRank;
			}
			else if(shand == "12345")
			{
				mRank = this.temp - 36;
				return mRank;
			}
			return 0;
		}
		
		// Params: n/a
		// Return: rank value of hand, else '0'
		// DESC: Validates the hand as a Full House and returns a rank value. 
		private function isFullHouse():int
		{
			//checks for full-house
			if (this.hand[0] == this.hand[1] && this.hand[3] == this.hand[4])
			{
				if (this.hand[0] == this.hand[2] || this.hand[2] == this.hand[4])
				{
					//return result
					if(this.hand[2] == "3"){ return 1; }
					else if(this.hand[2] == "4"){ return 2; }
					else if(this.hand[2] == "5"){ return 3; }
					else if(this.hand[2] == "6"){ return 4; }
					else if(this.hand[2] == "7"){ return 5; }
					else if(this.hand[2] == "8"){ return 6; }
					else if(this.hand[2] == "9"){ return 7; }
					else if(this.hand[2] == "0"){ return 8; }
					else if(this.hand[2] == "J"){ return 9; }
					else if(this.hand[2] == "Q"){ return 10; }
					else if(this.hand[2] == "K"){ return 11; }
					else if(this.hand[2] == "1"){ return 12; }
					else if(this.hand[2] == "2"){ return 13; }
				}
			}
			return 0;
		}

		// Params: n/a
		// Return: rank value of hand, else '0'
		// DESC: Validates the hand as a Four of a Kind and returns a rank value. 
		private function isFourOfAKind():int
		{
			//creates four-of-a-kind string
			if (this.hand[0] == this.hand[3] || this.hand[1] == this.hand[4])
			{
				//return result
				if(this.hand[2] == "3"){ return 1; }
				else if(this.hand[2] == "4"){ return 2; }
				else if(this.hand[2] == "5"){ return 3; }
				else if(this.hand[2] == "6"){ return 4; }
				else if(this.hand[2] == "7"){ return 5; }
				else if(this.hand[2] == "8"){ return 6; }
				else if(this.hand[2] == "9"){ return 7; }
				else if(this.hand[2] == "0"){ return 8; }
				else if(this.hand[2] == "J"){ return 9; }
				else if(this.hand[2] == "Q"){ return 10; }
				else if(this.hand[2] == "K"){ return 11; }
				else if(this.hand[2] == "1"){ return 12; }
				else if(this.hand[2] == "2"){ return 13; }
			}
			return 0;
		}

		// Params: n/a
		// Return: rank value of hand
		// DESC: Only public method of the Hand object, returning the hand value. 
		public function get rank():int
		{
			return mRank;
		}
	}
}